import { defHttp } from '/@/utils/http/axios';
import { isArray } from '/@/utils/is';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

export enum Api {
  onlCgreportParamList = '/api-online/online/cgreport/param/listByHeadId',
  onlCgreportItemList = '/api-online/online/cgreport/item/listByHeadId',
}

const list = (params) => defHttp.get({ url: '/api-online/online/cgreport/head/list', params });

const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: '/api-online/online/cgreport/head/delete', params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

const batchDelete = (params, handleSuccess) => {
  createConfirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    iconType: 'warning',
    onOk: () => {
      return defHttp.delete({ url: '/api-online/online/cgreport/head/deleteBatch', data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

const saveOrUpdate = (params, isUpdate) => {
  if (isUpdate) {
    return defHttp.put({ url: '/api-online/online/cgreport/head/editAll', params });
  } else {
    return defHttp.post({ url: '/api-online/online/cgreport/head/add', params });
  }
};

const getReportParam = (id) => {
  return defHttp.get({ url: '/api-online/online/cgreport/api/getParamsInfo/' + id });
};

const getDataSourceList = () => {
  return defHttp.get({ url: '/api-upms/sys/dataSource/options' });
};

const analyzeSql = (params) => {
  return defHttp.get({
    url: '/api-online/online/cgreport/head/parseSql?' + params,
  });
};

export const onlCgreportParamList = Api.onlCgreportParamList;
export const onlCgreportItemList = Api.onlCgreportItemList;
export { list, deleteOne, batchDelete, saveOrUpdate, getReportParam, getDataSourceList, analyzeSql };
